class MuseumsController < ApplicationController

  def index
    @museums = Museum.find(:all)

    respond_to do |format|
      format.html # index.rhtml
      format.xml  { render :xml => @museums.to_xml }
    end
  end

  def show
    @museum = Museum.find(params[:id])
    
    @countries = Country.find_by_sql(["select countries.*, count(entities.country_id) as entity_count from countries, entities where entities.country_id = countries.id AND entities.museum_id = ? group by entities.country_id order by entity_count DESC limit 0, 5", @museum.id])
    
    @top_object_types = ObjectType.find_by_sql("SELECT object_types.id, object_types.name, count(entities.id) as 'count' FROM `categorisations` LEFT JOIN entities ON categorisations.entity_id = entities.id LEFT JOIN object_types ON categorisations.object_type_id = object_types.id WHERE entities.museum_id = 1 GROUP BY categorisations.object_type_id ORDER BY count DESC LIMIT 0,5;")

    @top_years = Entity.find_by_sql(["select entities.acquired_year, count(entities.acquired_year) as count from entities where entities.acquired_year <> '' and entities.museum_id = ? group by entities.acquired_year order by count DESC limit 0, 5", @museum.id])


    respond_to do |format|
      format.html # show.rhtml
      format.xml  { render :xml => @museum.to_xml }
    end
  end
  
  def objects
    @museum = Museum.find(params[:id])
    @countries = Country.find(:all)
    
    @objects_with_year = Entity.find_by_sql(["SELECT COUNT(*) AS 'count'  FROM `entities` WHERE acquired_year IS NOT NULL and museum_id = ?", @museum.id])

    @objects_with_country = Entity.find_by_sql(["SELECT COUNT(*) AS 'count'  FROM `entities` WHERE country_id IS NOT NULL and museum_id = ?", @museum.id])

    @objects_with_both = Entity.find_by_sql(["SELECT COUNT(*) AS 'count'  FROM `entities` WHERE country_id IS NOT NULL and acquired_year IS NOT NULL and museum_id = ?", @museum.id])
    
  end
  
  def countries
    @museum = Museum.find(params[:id])
    @countries = Country.find_by_sql(["select countries.id, countries.name, count(entities.country_id) as entity_count from entities, countries where entities.museum_id = ? and entities.country_id = countries.id group by entities.country_id", params[:id]])
  end

  def collections
    @museum = Museum.find(params[:id])
    
    @countries = Country.find(:all)
    
    #@countries = Country.find_by_sql(["select countries.*, count(entities.country_id) as entity_count from countries, entities where entities.country_id = countries.id AND entities.museum_id = ? group by entities.country_id", @museum.id])

     render :layout => false
  end

  def import_collections
    @museum = Museum.find(params[:id])
    
    if request.post?
      
     
      flash[:notice] = @count.to_s + " object(s) were imported " + @message
      redirect_to museum_path(@museum)
    end
  end

  def new
    @museum = Museum.new
  end

  def edit
    @museum = Museum.find(params[:id])
  end

  def create
    @museum = Museum.new(params[:museum])

    respond_to do |format|
      if @museum.save
        format.html { redirect_to museum_url(@museum) }
        format.xml  { head :created, :location => museum_url(@museum) }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @museum.errors.to_xml }
      end
    end
  end

  def update
    @museum = Museum.find(params[:id])

    respond_to do |format|
      if @museum.update_attributes(params[:museum])
        format.html { redirect_to museum_url(@museum) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @museum.errors.to_xml }
      end
    end
  end

  def destroy
    @museum = Museum.find(params[:id])
    @museum.destroy

    respond_to do |format|
      format.html { redirect_to museums_url }
      format.xml  { head :ok }
    end
  end
end
